data    = read_excel(tmp, sheet = "RGDPX")
data$RGDPX_ZM      = as.numeric(data$RGDPX_ZM)
data$RGDPX_BP      = as.numeric(data$RGDPX_BP)
data$RGDPX_6M      = as.numeric(data$RGDPX_6M)
data$RGDPX_12M     = as.numeric(data$RGDPX_12M)
data$f1       = 100*((data$RGDPX_6M/data$RGDPX_BP)^(4/3)-1)
data$f1r      = 200*(data$RGDPX_6M/data$RGDPX_ZM-1)
data$f2r      = 200*(data$RGDPX_12M/data$RGDPX_6M-1)
data$quarter_h1                    = data$quarter
data$quarter_h1[data$quarter == 2] = 4
data$quarter_h1[data$quarter == 4] = 2
data$year_h1                       = data$year
data$year_h1[data$quarter == 2]    = data$year[data$quarter == 2]
data$year_h1[data$quarter == 4]    = data$year[data$quarter == 4]+1
data$quarter_h2 = data$quarter
data$year_h2    = data$year + 1
data$id         = data$ID
data_f1          = subset(data, select = c(year_h1,quarter_h1,id,f1,f1r) )
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,f1,f1r) )
data_f2          = subset(data, select = c(year_h2,quarter_h2,id,f2r) )
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,f2r) )
data_clean       = merge(data_f1, data_f2, all = "TRUE")
tmp             = paste(indata,"data_realizations.xlsx",sep="")
data_tmp        = read_excel(tmp)
data_tmp        = subset(data_tmp, select = c(year,quarter, y, fly))
data_clean      = merge(data_clean, data_tmp, all = "TRUE")
data            = data_clean
data$fc_err     = data$y-data$f1
data$fc_rev     = data$f1r-data$f2r
data$y_realz    = data$y
data$f1         = data$f1
data$f2         = data$f2r
data$ID         = data$id
data$qdate      = data$year + data$quarter/4-0.25
data            = data[data$year>=1971,]
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,y_realz,fc_err,fc_rev))
save(data,file="us_liv_rgdp_ind.rda")
clear()
##### LIV CPI #########
(WD <- getwd())
if (!is.null(WD)) setwd(WD)
indata      = paste0(WD,"/_liv/","", collapse = NULL)
tmp     = paste(indata,"data_liv_ind.xlsx",sep="")
data    = read_excel(tmp, sheet = "CPI")
data$CPI_BP      = as.numeric(data$CPI_BP)
data$CPI_ZM      = as.numeric(data$CPI_ZM)
data$CPI_6M      = as.numeric(data$CPI_6M)
data$CPI_12M     = as.numeric(data$CPI_12M)
data$cat           = data$Category
data$f1       = 100*((data$CPI_6M/data$CPI_BP)^(4/3)-1)
data$f1r      = 200*(data$CPI_6M/data$CPI_ZM-1)
data$f2r      = 200*(data$CPI_12M/data$CPI_6M-1)
data$quarter_h1                    = data$quarter
data$quarter_h1[data$quarter == 2] = 4
data$quarter_h1[data$quarter == 4] = 2
data$year_h1                       = data$year
data$year_h1[data$quarter == 2]    = data$year[data$quarter == 2]
data$year_h1[data$quarter == 4]    = data$year[data$quarter == 4]+1
data$quarter_h2 = data$quarter
data$year_h2    = data$year + 1
data$id         = data$ID
data_f1          = subset(data, select = c(year_h1,quarter_h1,id,f1,f1r, cat))
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,f1,f1r, cat))
data_f2          = subset(data, select = c(year_h2,quarter_h2,id,f2r))
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,f2r))
data_clean       = merge(data_f1, data_f2, all = "TRUE")
data_clean$fc_rev= data_clean$f2r-data_clean$f1r
tmp             = paste(indata,"data_realizations.xlsx",sep="")
data_tmp        = read_excel(tmp)
data_tmp        = subset(data_tmp, select = c(year,quarter,pi,flpi))
data_clean      = merge(data_clean, data_tmp, all = "TRUE")
data            = data_clean
data$fc_err     = data$pi-data$f1
data$p_realz    = data$pi
data$f1         = data$f1r
data$f2         = data$f2r
data$ID         = data$id
data$qdate      = data$year + data$quarter/4-0.25
data            = data[data$year>=1971,]
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,p_realz,fc_err,fc_rev,flpi, cat))
save(data,file="us_liv_cpi_ind.rda")
clear()
# LIV DATA Clean Individual
rm(list=ls())
(WD <- getwd())
if (!is.null(WD)) setwd(WD)
indata      = paste0(WD,"/_liv/","", collapse = NULL)
library(haven)
library(AER)
library(sandwich)
library(lmtest)
library(pracma)
library(stargazer)
library(plm)
library(pracma)
library(DataCombine)
library(jtools)
library(plyr)
library(readxl)
lagpad   = function(x, k) {
if (!is.vector(x))
stop('x must be a vector')
if (!is.numeric(x))
stop('x must be numeric')
if (!is.numeric(k))
stop('k must be numeric')
if (1 != length(k))
stop('k must be a single number')
c(rep(NA, k), x)[1 : length(x)]
}
##### LIV GDP #########
tmp     = paste(indata,"data_liv_ind.xlsx",sep="")
data    = read_excel(tmp, sheet = "RGDPX")
data$RGDPX_ZM      = as.numeric(data$RGDPX_ZM)
data$RGDPX_BP      = as.numeric(data$RGDPX_BP)
data$RGDPX_6M      = as.numeric(data$RGDPX_6M)
data$RGDPX_12M     = as.numeric(data$RGDPX_12M)
data$f1       = 100*((data$RGDPX_6M/data$RGDPX_BP)^(4/3)-1)
data$f1r      = 200*(data$RGDPX_6M/data$RGDPX_ZM-1)
data$f2r      = 200*(data$RGDPX_12M/data$RGDPX_6M-1)
data$quarter_h1                    = data$quarter
data$quarter_h1[data$quarter == 2] = 4
data$quarter_h1[data$quarter == 4] = 2
data$year_h1                       = data$year
data$year_h1[data$quarter == 2]    = data$year[data$quarter == 2]
data$year_h1[data$quarter == 4]    = data$year[data$quarter == 4]+1
data$quarter_h2 = data$quarter
data$year_h2    = data$year + 1
data$id         = data$ID
data_f1          = subset(data, select = c(year_h1,quarter_h1,id,f1,f1r) )
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,f1,f1r) )
data_f2          = subset(data, select = c(year_h2,quarter_h2,id,f2r) )
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,f2r) )
data_clean       = merge(data_f1, data_f2, all = "TRUE")
tmp             = paste(indata,"data_realizations.xlsx",sep="")
data_tmp        = read_excel(tmp)
data_tmp        = subset(data_tmp, select = c(year,quarter, y, fly))
data_clean      = merge(data_clean, data_tmp, all = "TRUE")
data            = data_clean
data$fc_err     = data$y-data$f1
data$fc_rev     = data$f1r-data$f2r
data$y_realz    = data$y
data$f1         = data$f1
data$f2         = data$f2r
data$ID         = data$id
data$qdate      = data$year + data$quarter/4-0.25
data            = data[data$year>=1971,]
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,y_realz,fc_err,fc_rev))
save(data,file="us_liv_rgdp_ind.rda")
clear()
##### LIV CPI #########
(WD <- getwd())
if (!is.null(WD)) setwd(WD)
indata      = paste0(WD,"/_liv/","", collapse = NULL)
tmp     = paste(indata,"data_liv_ind.xlsx",sep="")
data    = read_excel(tmp, sheet = "CPI")
data$CPI_BP      = as.numeric(data$CPI_BP)
data$CPI_ZM      = as.numeric(data$CPI_ZM)
data$CPI_6M      = as.numeric(data$CPI_6M)
data$CPI_12M     = as.numeric(data$CPI_12M)
data$f1       = 100*((data$CPI_6M/data$CPI_BP)^(4/3)-1)
data$f1r      = 200*(data$CPI_6M/data$CPI_ZM-1)
data$f2r      = 200*(data$CPI_12M/data$CPI_6M-1)
data$quarter_h1                    = data$quarter
data$quarter_h1[data$quarter == 2] = 4
data$quarter_h1[data$quarter == 4] = 2
data$year_h1                       = data$year
data$year_h1[data$quarter == 2]    = data$year[data$quarter == 2]
data$year_h1[data$quarter == 4]    = data$year[data$quarter == 4]+1
data$quarter_h2 = data$quarter
data$year_h2    = data$year + 1
data$id         = data$ID
data_f1          = subset(data, select = c(year_h1,quarter_h1,id,f1,f1r))
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,f1,f1r))
data_f2          = subset(data, select = c(year_h2,quarter_h2,id,f2r))
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,f2r))
data_clean       = merge(data_f1, data_f2, all = "TRUE")
data_clean$fc_rev= data_clean$f2r-data_clean$f1r
tmp             = paste(indata,"data_realizations.xlsx",sep="")
data_tmp        = read_excel(tmp)
data_tmp        = subset(data_tmp, select = c(year,quarter,pi,flpi))
data_clean      = merge(data_clean, data_tmp, all = "TRUE")
data            = data_clean
data$fc_err     = data$pi-data$f1
data$p_realz    = data$pi
data$f1         = data$f1r
data$f2         = data$f2r
data$ID         = data$id
data$qdate      = data$year + data$quarter/4-0.25
data            = data[data$year>=1971,]
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,p_realz,fc_err,fc_rev,flpi))
save(data,file="us_liv_cpi_ind.rda")
clear()
# EA SPF DATA CPI Clean
rm(list=ls())
(WD <- getwd())
if (!is.null(WD)) setwd(WD)
indata      = paste0(WD,"/_easpf/","", collapse = NULL)
library(haven)
library(AER)
library(sandwich)
library(lmtest)
library(pracma)
library(stargazer)
library(plm)
library(pracma)
library(DataCombine)
library(jtools)
library(plyr)
library(readxl)
library(tidyr)
library(robustHD)
###### CLEAN CPI ######
for (ii in 1999:2020) {
for (jj in 1:4){
tmp   = paste(indata,paste0(paste0(paste0(ii,"Q"),jj),".csv"),sep="")
data_tmp = read.csv(tmp, skip = 1)
data_tmp$id       = data_tmp$FCT_SOURCE
data_tmp$target   = data_tmp$TARGET_PERIOD
data_tmp$forecast = data_tmp$POINT
data_tmp = subset(data_tmp, select = c(id,target,forecast))
ind               = grep("^CORE",data_tmp$target)
data_inf          = data_tmp[1:ind[1]-1,]
data_inf          = spread(data_inf, target, forecast)
data_inf          = data_inf[-c(1),]
data_inf          = data_inf[c(1,4,6)]
names(data_inf)[2] = "forecast1"
names(data_inf)[3] = "forecast2"
data_inf$year    = ii
data_inf$quarter = jj
data  = data_inf
tmp   = paste0(paste0(paste0(ii,"Q"),jj),".rda")
save(data, file = tmp)
}
}
rm(list=ls())
for (ii in 1999:2020) {
for (jj in 1:4){
tmp   = paste0(paste0(paste0(ii,"Q"),jj),".rda")
load(tmp)
data_tmp = data
if (ii == 1999 && jj == 1 ) {
data_final  = data_tmp
} else {
data_final = merge(data_final,data_tmp, all = "TRUE")
}
}
}
data              = data_final
data$forecast1    = as.numeric(levels(data$forecast1))[as.integer(data$forecast1)]
data$forecast2    = as.numeric(levels(data$forecast2))[as.integer(data$forecast2)]
data$id           = as.numeric(data$id)
data = data[order(data$year,data$quarter),]
data$quarter_h1                    = data$quarter -1
data$quarter_h1[data$quarter == 1] = 4
data$year_h1                       = data$year + 1
data$year_h1[data$quarter == 1]    = data$year[data$quarter == 1]
data$quarter_h2 = data$quarter_h1
data$year_h2    = data$year_h1+1
data_f1          = subset(data, select = c(year_h1,quarter_h1,id,forecast1) )
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,forecast1) )
data_f2          = subset(data, select = c(year_h2,quarter_h2,id,forecast2) )
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,forecast2) )
data_clean       = merge(data_f1, data_f2, all = "TRUE")
data_inf        = read_excel("_easpf/data_realizations.xlsx", sheet = "Export")
data_inf        = subset(data_inf, select = c(year,quarter, rcpi_yoy,flpi) )
data_clean      = merge(data_clean, data_inf)
###### SAVE THE DATA CPI ########
data            = data_clean
data$qdate      = data$year + data$quarter/4-0.25
data$fc_err     = data$rcpi_yoy-data$forecast1
data$fc_rev     = data$forecast1-data$forecast2
data$f1         = data$forecast1
data$f2         = data$forecast2
data$p_realz    = data$rcpi_yoy
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,p_realz,fc_err,fc_rev))
data            = data[data$year>="2000",]
save(data,file="ea_spf_cpi_ind.rda")
clear()
# EA SPF DATA RGDP Clean
rm(list=ls())
(WD <- getwd())
if (!is.null(WD)) setwd(WD)
indata      = paste0(WD,"/_easpf/","", collapse = NULL)
library(haven)
library(AER)
library(sandwich)
library(lmtest)
library(pracma)
library(stargazer)
library(plm)
library(pracma)
library(DataCombine)
library(jtools)
library(plyr)
library(readxl)
library(tidyr)
###### CLEAN GDP ######
for (ii in 1999:2020) {
for (jj in 1:4){
tmp   = paste(indata,paste0(paste0(paste0(ii,"Q"),jj),".csv"),sep="")
data_tmp = read.csv(tmp, skip = 1)
data_tmp$id       = data_tmp$FCT_SOURCE
data_tmp$target   = data_tmp$TARGET_PERIOD
data_tmp$forecast = data_tmp$POINT
data_tmp = subset(data_tmp, select = c(id,target,forecast))
ind               = grep("^GROWTH",data_tmp$target)
ind1              = grep("^EXPECTED",data_tmp$target)
ind               = ind+2
ind1              = ind1-2
data_gdp          = data_tmp[ind:ind1,]
data_gdp          = spread(data_gdp, target, forecast)
data_gdp          = data_gdp[c(1,3,5)]
names(data_gdp)[2] = "forecast1"
names(data_gdp)[3] = "forecast2"
data_gdp$year    = ii
data_gdp$quarter = jj
data  = data_gdp
tmp   = paste0(paste0(paste0(ii,"Q"),jj),".rda")
save(data, file = tmp)
}
}
rm(list=ls())
for (ii in 1999:2020) {
for (jj in 1:4){
tmp   = paste0(paste0(paste0(ii,"Q"),jj),".rda")
load(tmp)
data_tmp = data
if (ii == 1999 && jj == 1 ) {
data_final  = data_tmp
} else {
data_final = merge(data_final,data_tmp, all = "TRUE")
}
}
}
data              = data_final
data$forecast1    = as.numeric(levels(data$forecast1))[as.integer(data$forecast1)]
data$forecast2    = as.numeric(levels(data$forecast2))[as.integer(data$forecast2)]
data$id           = as.numeric(data$id)
data = data[order(data$year,data$quarter),]
data$quarter_h1[data$quarter == 1] = 3
data$quarter_h1[data$quarter == 2] = 4
data$quarter_h1[data$quarter == 3] = 1
data$quarter_h1[data$quarter == 4] = 2
data$year_h1                       = data$year + 1
data$year_h1[data$quarter == 1]    = data$year[data$quarter == 1]
data$year_h1[data$quarter == 2]    = data$year[data$quarter == 2]
data$quarter_h2  = data$quarter_h1
data$year_h2     = data$year_h1+1
data_f1          = subset(data, select = c(year_h1,quarter_h1,id,forecast1))
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,forecast1))
data_f2          = subset(data, select = c(year_h2,quarter_h2,id,forecast2))
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,forecast2))
data_clean       = merge(data_f1, data_f2, all = "TRUE")
data_gdp        = read_excel("_easpf/data_realizations.xlsx", sheet = "Export")
data_gdp        = subset(data_gdp, select = c(year,quarter, rgdp_yoy,fly) )
data_clean      = merge(data_clean, data_gdp)
###### SAVE THE DATA GDP ########
data            = data_clean
data$qdate      = data$year + data$quarter/4-0.25
data$fc_err     = data$rgdp_yoy-data$forecast1
data$fc_rev     = data$forecast1-data$forecast2
data$f1         = data$forecast1
data$f2         = data$forecast2
data$y_realz    = data$rgdp_yoy
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,y_realz,fc_err,fc_rev))
data            = data[data$year>="2000",]
save(data,file="ea_spf_rgdp_ind.rda")
clear()
# LIV DATA Clean Individual
rm(list=ls())
(WD <- getwd())
if (!is.null(WD)) setwd(WD)
indata      = paste0(WD,"/_liv/","", collapse = NULL)
library(haven)
library(AER)
library(sandwich)
library(lmtest)
library(pracma)
library(stargazer)
library(plm)
library(pracma)
library(DataCombine)
library(jtools)
library(plyr)
library(readxl)
lagpad   = function(x, k) {
if (!is.vector(x))
stop('x must be a vector')
if (!is.numeric(x))
stop('x must be numeric')
if (!is.numeric(k))
stop('k must be numeric')
if (1 != length(k))
stop('k must be a single number')
c(rep(NA, k), x)[1 : length(x)]
}
##### LIV GDP #########
tmp     = paste(indata,"data_liv_ind.xlsx",sep="")
data    = read_excel(tmp, sheet = "RGDPX")
data$RGDPX_ZM      = as.numeric(data$RGDPX_ZM)
data$RGDPX_BP      = as.numeric(data$RGDPX_BP)
data$RGDPX_6M      = as.numeric(data$RGDPX_6M)
data$RGDPX_12M     = as.numeric(data$RGDPX_12M)
data$f1       = 100*((data$RGDPX_6M/data$RGDPX_BP)^(4/3)-1)
data$f1r      = 200*(data$RGDPX_6M/data$RGDPX_ZM-1)
data$f2r      = 200*(data$RGDPX_12M/data$RGDPX_6M-1)
data$quarter_h1                    = data$quarter
data$quarter_h1[data$quarter == 2] = 4
data$quarter_h1[data$quarter == 4] = 2
data$year_h1                       = data$year
data$year_h1[data$quarter == 2]    = data$year[data$quarter == 2]
data$year_h1[data$quarter == 4]    = data$year[data$quarter == 4]+1
data$quarter_h2 = data$quarter
data$year_h2    = data$year + 1
data$id         = data$ID
data_f1          = subset(data, select = c(year_h1,quarter_h1,id,f1,f1r) )
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,f1,f1r) )
data_f2          = subset(data, select = c(year_h2,quarter_h2,id,f2r) )
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,f2r) )
data_clean       = merge(data_f1, data_f2, all = "TRUE")
tmp             = paste(indata,"data_realizations.xlsx",sep="")
data_tmp        = read_excel(tmp)
data_tmp        = subset(data_tmp, select = c(year,quarter, y, fly))
data_clean      = merge(data_clean, data_tmp, all = "TRUE")
data            = data_clean
data$fc_err     = data$y-data$f1
data$fc_rev     = data$f1r-data$f2r
data$y_realz    = data$y
data$f1         = data$f1
data$f2         = data$f2r
data$ID         = data$id
data$qdate      = data$year + data$quarter/4-0.25
data            = data[data$year>=1971,]
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,y_realz,fc_err,fc_rev))
save(data,file="us_liv_rgdp_ind.rda")
clear()
##### LIV CPI #########
(WD <- getwd())
if (!is.null(WD)) setwd(WD)
indata      = paste0(WD,"/_liv/","", collapse = NULL)
tmp     = paste(indata,"data_liv_ind.xlsx",sep="")
data    = read_excel(tmp, sheet = "CPI")
data$CPI_BP      = as.numeric(data$CPI_BP)
data$CPI_ZM      = as.numeric(data$CPI_ZM)
data$CPI_6M      = as.numeric(data$CPI_6M)
data$CPI_12M     = as.numeric(data$CPI_12M)
data$f1       = 100*((data$CPI_6M/data$CPI_BP)^(4/3)-1)
data$f1r      = 200*(data$CPI_6M/data$CPI_ZM-1)
data$f2r      = 200*(data$CPI_12M/data$CPI_6M-1)
data$quarter_h1                    = data$quarter
data$quarter_h1[data$quarter == 2] = 4
data$quarter_h1[data$quarter == 4] = 2
data$year_h1                       = data$year
data$year_h1[data$quarter == 2]    = data$year[data$quarter == 2]
data$year_h1[data$quarter == 4]    = data$year[data$quarter == 4]+1
data$quarter_h2 = data$quarter
data$year_h2    = data$year + 1
data$id         = data$ID
data_f1          = subset(data, select = c(year_h1,quarter_h1,id,f1,f1r))
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,f1,f1r))
data_f2          = subset(data, select = c(year_h2,quarter_h2,id,f2r))
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,f2r))
data_clean       = merge(data_f1, data_f2, all = "TRUE")
data_clean$fc_rev= data_clean$f2r-data_clean$f1r
tmp             = paste(indata,"data_realizations.xlsx",sep="")
data_tmp        = read_excel(tmp)
data_tmp        = subset(data_tmp, select = c(year,quarter,pi,flpi))
data_clean      = merge(data_clean, data_tmp, all = "TRUE")
data            = data_clean
data$fc_err     = data$pi-data$f1
data$p_realz    = data$pi
data$f1         = data$f1r
data$f2         = data$f2r
data$ID         = data$id
data$qdate      = data$year + data$quarter/4-0.25
data            = data[data$year>=1971,]
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,p_realz,fc_err,fc_rev,flpi))
save(data,file="us_liv_cpi_ind.rda")
clear()
